﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace NutriLeaf.Phone.Sync
{
    public class NutriLeafSync
    {
        static Settings.Settings setting = new Settings.Settings();

        #region User
        public static event EventHandler UserSyncCompleted;
        static UserSync.UserSyncOfflineContext UserContext = new UserSync.UserSyncOfflineContext(Constants.SyncCache.User, new Uri(Constants.SyncServices.User));
        public static void SyncUser()
        {
            if (setting.User.UserID == 0 || setting.User.UserID == null)
            {
                throw new ArgumentNullException("UserID in setting file not found");
            }


            UserContext.CacheController.ControllerBehavior.AddScopeParameters("UserID", setting.User.UserID.ToString());
            UserContext.CacheController.ControllerBehavior.SerializationFormat = Microsoft.Synchronization.ClientServices.SerializationFormat.ODataJSON;
            UserContext.LoadCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs>(UserContext_LoadCompleted);
            UserContext.LoadAsync();
        }

        static void UserContext_LoadCompleted(object sender, Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs e)
        {
            UserContext.CacheController.RefreshCompleted +=new EventHandler<Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs>(CacheController_UserRefreshCompleted);
            UserContext.CacheController.RefreshAsync();
        }


        static void CacheController_UserRefreshCompleted(object sender, Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs e)
        {
            UserContext.Close();
            EventHandler handler = UserSyncCompleted;
            if (null != handler) handler(null, EventArgs.Empty);
            
        }

        #endregion 

        #region Tips
        public static event EventHandler TipSyncCompleted;
        static TipSync.TipSyncOfflineContext TipContext = new TipSync.TipSyncOfflineContext(Constants.SyncCache.Tips, new Uri(Constants.SyncServices.Tips));
        public static void SyncTips()
        {
            if (setting.User.UserID == 0 || setting.User.UserID == null)
            {
                throw new ArgumentNullException("UserID in setting file not found");
            }

            TipContext.CacheController.ControllerBehavior.AddScopeParameters("UserID", setting.User.UserID.ToString());
            TipContext.CacheController.ControllerBehavior.SerializationFormat = Microsoft.Synchronization.ClientServices.SerializationFormat.ODataJSON;
            TipContext.LoadCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs>(TipContext_LoadCompleted);
            TipContext.LoadAsync();
        }

        static void TipContext_LoadCompleted(object sender, Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs e)
        {
            TipContext.CacheController.RefreshCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs>(CacheController_TipRefreshCompleted);
            TipContext.CacheController.RefreshAsync();
        }


        static void CacheController_TipRefreshCompleted(object sender, Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs e)
        {
            TipContext.Close();
            EventHandler handler = TipSyncCompleted;
            if (null != handler) handler(null, EventArgs.Empty);
        }

        #endregion 

        #region Articles
        public static event EventHandler ArticleSyncCompleted;
        static ArticleSync.ArticleOfflineContext ArticleContext = new ArticleSync.ArticleOfflineContext(Constants.SyncCache.Articles, new Uri(Constants.SyncServices.Article));
        public static void SyncArticles()
        {
            if (setting.User.UserID == 0 || setting.User.UserID == null)
            {
                throw new ArgumentNullException("UserID in setting file not found");
            }

            ArticleContext.CacheController.ControllerBehavior.AddScopeParameters("UserID", setting.User.UserID.ToString());
            ArticleContext.CacheController.ControllerBehavior.SerializationFormat = Microsoft.Synchronization.ClientServices.SerializationFormat.ODataJSON;
            ArticleContext.LoadCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs>(ArticleContext_LoadCompleted);
            ArticleContext.LoadAsync();
        }

        static void ArticleContext_LoadCompleted(object sender, Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs e)
        {
            ArticleContext.CacheController.RefreshCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs>(CacheController_ArticleRefreshCompleted);
            ArticleContext.CacheController.RefreshAsync();
        }


        static void CacheController_ArticleRefreshCompleted(object sender, Microsoft.Synchronization.ClientServices.RefreshCompletedEventArgs e)
        {
            ArticleContext.Close();
            EventHandler handler = ArticleSyncCompleted;
            if (null != handler) handler(null, EventArgs.Empty);
        }


        #endregion



    }
}
